//
//  iMagicViewController.m
//  iMagic
//
//  Created by Tang Xiaoping on 6/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "iMagicViewController.h"
#import "CustomDrawView.h"

@implementation iMagicViewController

@synthesize delegate;

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
	
	//add gesture
	UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
	[tapRecognizer setNumberOfTapsRequired:1];
	[tapRecognizer setDelegate:self];
	//tapRecognizer.delaysTouchesBegan = YES;
	[self.view addGestureRecognizer:tapRecognizer];
	[tapRecognizer release];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
	
    // Disallow recognition of tap gestures in the button.
	if ((theButton && theButton.hidden == NO) || (backButton && backButton.hidden == NO)) {
		return NO;
	}
    return YES;
}

-(void)tapped:(id)sender {
	NSLog(@"See a tap gesture");
	if (theView == nil) {
		theView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
		
		[self.view addSubview:theView];
		[theView release];
	}
	
	if (theButton == nil) {
		UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		button.frame = CGRectMake(0, 247, 320, 37);
		[button setTitle:@"Already Remember Image" forState:UIControlStateNormal];
		[button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
		
		[self.view addSubview:button];
		theButton = button;
		
		//not remember button
		backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		backButton.frame = CGRectMake(0, 180, 320, 37);
		[backButton setTitle:@"Not Remember, See Again" forState:UIControlStateNormal];
		[backButton addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside];
		
		[self.view addSubview:backButton];
		
	}
	
	theView.backgroundColor = [UIColor blackColor];
	theView.alpha  = 0.5;
	
	theView.hidden = NO;
	theButton.hidden = NO;
	backButton.hidden = NO;
}

- (void)backPressed
{
	theView.hidden = YES;
	theButton.hidden = YES;
	imageView.hidden = YES;
	backButton.hidden = YES;
}

-(void)buttonPressed {
	NSLog(@"Button Pressed!");
	if ([theButton.titleLabel.text isEqualToString:@"Already Remember Image"]) {
		if (imageView == nil) {
			imageView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 150, 28, 28)];
			[self.view addSubview:imageView];
			[imageView release];
		}
		
		imageView.image = [(CustomDrawView*)self.view getYourImage];
		[theButton setTitle:@"Play Again" forState:UIControlStateNormal];
		
		theView.backgroundColor = [UIColor groupTableViewBackgroundColor];
		theView.alpha = 1.0;
		
		imageView.hidden = NO;
		backButton.hidden = YES;
	}
	else {
		if ([delegate respondsToSelector:@selector(backMainView)]) {
			[delegate backMainView];
		}
		
		[theButton setTitle:@"Already Remember Image" forState:UIControlStateNormal];
		
		theView.hidden = YES;
		theButton.hidden = YES;
		imageView.hidden = YES;
		backButton.hidden = YES;
	}
	
}

- (IBAction)refresh:(id)sender
{
	if ([self.view respondsToSelector:@selector(randomImage)]) {
		[self.view performSelector:@selector(randomImage)];
	}
	
	[self.view setNeedsDisplay];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
